#include <stdio.h>
#include "debug.h"
#include "bleuart.h"
#include "gpioconfig.h"

uint8_t BleRxData[20];
uint8_t count;
uint8_t bleReceiveState = 0;
uint8_t tagLen = 0;
uint8_t dataLen = 0;
uint8_t dataIndex = 0;

void Uart_BleInit(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_USART2TX;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_USART2RX;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

    USART_Init(USART2, &USART_InitStructure);
    USART_Cmd(USART2, ENABLE);
}

void BleReceiverData()
{
    if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE)) {
        uint8_t bleData = (uint8_t)USART_ReceiveData(USART2);
        if (bleData & 0x1F == 0x1F) {
            count = 0;
            uint8_t index = 0;
            BleRxData[count++] = bleData;
        }
    }
}

uint8_t getTagBytesCount(uint8_t *p, uint8_t *dataIndex)
{
    uint8_t  len = 0;
    if (*p & 0x1F == 0x1F) {
        len = 2;
        dataIndex += len;
    } else {
        len = 1;
        dataIndex += len;
    }
    return len;
}

uint8_t getLengthBytesCount(uint8_t *p, uint8_t *dataIndex)
{
    uint8_t len = 0;
    if (*(p+*dataIndex) & 0x80 == 0x80) {
        len = 1 + (*(p+*dataIndex) & 0x7f);
    } else {
        len = 1;
    }
    return len;
}

uint8_t getDataLength(uint8_t *p, uint8_t *dataIndex)
{
    uint8_t length = 0;
    length = *(p+*dataIndex);
    if((length & 0x80) == 0x80) {
        int numberOfBytes = length & 0x7f;
        length = 0;
        for(int i=*dataIndex+1; i<*dataIndex+1+numberOfBytes; i++) {
            length = length * 0x100 + (*(p+*dataIndex) & 0xff);
        }
    }
    return length;
}

void ReceiverDataParse(void)
{
    uint8_t flag = 0;
    if (getTagBytesCount(BleRxData, &dataIndex) == 1) {
        if (BleRxData[1] == 0x01) {
            printf("Module:0x01");
            flag = 1;
        } else {
            printf("Module:error");
        }
    } else {
        printf("Module:Double Error");
    }
    if (flag == 1) {
        if (getLengthBytesCount(BleRxData, &dataIndex) == 1) {
            if (BleRxData[dataIndex+1] == 0x00) {
                printf("OPEN");
            } else if (BleRxData[dataIndex+1] == 0x01) {
                printf("CLOSED");
            }
        }
    }
}

void PrintfBleRxData(uint8_t *BleRxData, uint8_t len)
{
    printf("Receive Data");
    for (uint8_t i = 0; i < len; i++) {
        printf(":%02x", BleRxData[i]);
    }
    printf("\r\n");
}
